package com.gome.boot.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 文件工具类.
 *
 * @author baoxiufeng
 */
public final class FileUtils {
    /**
     * 日志输出类.
     */
    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 文件读写锁
     */
    private static ReadWriteLock lock = new ReentrantReadWriteLock();

    /**
     * 读取相对路径下属性配置文件内容.
     *
     * @param filePath 待读取的文件路径
     * @return 属性配置文件内容
     */
    public static Properties getRelativePathProperties(String filePath) {
        return getProperties(FileUtils.class.getClassLoader().getResourceAsStream(filePath));
    }

    /**
     * 读取绝对路径下属性配置文件内容.
     *
     * @param filePath 待读取的文件路径
     * @return 属性配置文件内容
     */
    public static Properties getAbsolutePathProperties(String filePath) {
        try {
            return getProperties(new FileInputStream(filePath));
        } catch (IOException e) {
            logger.error("[FileUtils.getAbsolutePathProperties] 属性配置文件读取失败:{}", e.getMessage());
        }
        return new Properties();
    }

    /**
     * 读取给定绝对路径的文件内容.
     *
     * @param filePath 待读取的文件路径
     * @return 读取到的文件内容
     */
    public static String readFileContent(String filePath) {
        String diamondUrl = null;
        File file = new File(filePath);
        if (file.exists()) {
            lock.readLock().lock();
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = br.readLine()) != null) {
                    line = CommonUtils.trimToEmpty(line);
                    if (!line.startsWith("#")) {
                        diamondUrl = CommonUtils.trim(line);
                        break;
                    }
                }
            } catch (IOException e) {
                logger.error("[FileUtils.readFileContent] 加载.diamond.domain文件[{}]出错:{}", filePath, e.getMessage());
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        ;
                    }
                }
                lock.readLock().unlock();
            }
        } else {
            logger.error("[FileUtils.readFileContent] 配置文件{}不存在，请检查！", filePath);
        }
        return diamondUrl;
    }

    /**
     * 加载并获取属性配置文件内容.
     *
     * @param is 文件输入流
     * @return 属性配置文件内容
     */
    private static Properties getProperties(InputStream is) {
        Properties props = new Properties();
        if (is != null) {
            try {
                props.load(is);
            } catch (IOException e) {
                logger.error("[FileUtils.getProperties] 属性配置文件加载失败:{}", e.getMessage());
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    ;
                }
            }
        }
        return props;
    }
}
